import Vuex from "vuex"
import Vue from "vue"
import {
     user_detail
} from "../network/user"
import {
     playlist_detail
} from "../network/login";
import {
     getSongDetial
} from "../network/play";
import {
     album
} from '../network/discover'
import {
     check_music
} from '../network/play'
import{
     dj_program_detail
} from "../network/dj"
import {msg_private} from "../network/user"
import {detail} from "../network/discover"
Vue.use(Vuex)
export default new Vuex.Store({
     state: {
          profile: window.localStorage.getItem("profile"), //头像
          nickName: window.localStorage.getItem("nickName"), //昵称
          userId: window.localStorage.getItem("userId"), //我的id
          followeds : window.localStorage.getItem("followeds"), //粉丝
          follows  : window.localStorage.getItem("follows"), //关注
          eventCount : window.localStorage.getItem("eventCount"), //动态
          music_id: '', //歌曲id
          album_content: null, //专辑详情
          newMsgCount:window.localStorage.getItem("newMsgCount"),  //通知消息数量
          commentsIds:null,
          subMusic:null, //收藏的音乐
          toplistSidebarData:[]  //排行榜榜单数据
     },
     mutations: {
          CLEAR(state) {   //退出登录
               state.profile = null
               state.userId = null
               state.nickName = null
               localStorage.clear()
          },
          submusic(state , data){  //收藏的歌曲
              state.subMusic = data
              console.log(data)
          },
          song_ids(state, data) { //发送歌单中歌曲id
               state.music_id = data
          },
          songAll_ids(state , data){ //发送歌单中筛选后的歌曲id
               if(state.music_id.song_id == data.song_id){ return }
               state.music_id = data
          },
          musicID(state, data) { //发送歌曲id
               state.music_id = data
          },
          album_ids(state, data) { //发送专辑中歌曲id
               state.music_id = data
          },
          dj_ids(state , data) { //发送电台id
               state.music_id = data
          },
          comments_Ids(state , ids){ //各个评论的id
               state.commentsIds = ids
          }

     },
     actions: {
          toplist(comments){
               detail().then(res => {
                 comments.state.toplistSidebarData = res.data.list
               })
          },
          private(comments , par){  //获取通知数目
               msg_private(par.limit , par.offset).then(res => {
                     console.log(res.data.newMsgCount)
                     comments.state.newMsgCount = res.data.newMsgCount
                     localStorage.setItem("newMsgCount" ,res.data.newMsgCount)
               })
          },
          user(comments, uid) { //异步获取我的信息 关注 粉丝 动态等
               user_detail(uid).then(res => {
                    comments.state.profile = res.data.profile.avatarUrl     //头像
                    localStorage.setItem("profile" ,res.data.profile.avatarUrl)
                    comments.state.nickName = res.data.profile.nickname     //昵称
                    localStorage.setItem("nickName" ,res.data.profile.nickname)
                    comments.state.userId = res.data.profile.userId         //我的id
                    localStorage.setItem("userId" ,res.data.profile.userId)
                    comments.state.followeds = res.data.profile.followeds       //粉丝
                    localStorage.setItem("followeds" ,res.data.profile.followeds)
                    comments.state.follows = res.data.profile.follows      //关注
                    localStorage.setItem("follows" ,res.data.profile.follows)
                    comments.state.eventCount = res.data.profile.eventCount      //动态
                    localStorage.setItem("eventCount" ,res.data.profile.eventCount)
               })
          },
          song_ct(comments, song_id) { //获取歌单详情
               if (comments.state.music_id.song_id == song_id) {
                    return
               }
               var song_ids = {
                    name: '歌单',
                    ids: [],
                    song_id: song_id
               }
               playlist_detail(song_id)
                    .then(res => {
                         res.data.playlist.tracks.forEach((item, index) => {
                              //1vip专属  4版权方要求，当前专辑需单独付费 筛选掉
                             if ((parseInt(item.fee) < 1 || parseInt(item.fee) > 4 ) && !item.noCopyrightRcmd) {
                                   song_ids.ids.push(item)
                              }
                         });
                         comments.commit('song_ids', song_ids)
                    })
               },
          music_ct(comments, ms_id) {   //获取歌曲详情
               var ids = ms_id.music_id
               if (comments.state.music_id.music_id === ids) {
                    return
               }
               var MS_ids = {
                    name: '歌曲',
                    ids: {},
                    music_id: ids
               }
               getSongDetial(ids).then(res => {
                    let ThisSong = MS_ids.ids =  res.data.songs[0] //筛选音乐播放器要用的内容
                    if (ThisSong.fee == 1) {
                         this._vm.$VIP.show()
                    } else if (ThisSong.fee == 4) {
                         this._vm.$PAY.show(ms_id.pageY)
                    } else if(ThisSong.noCopyrightRcmd){
                         this._vm.$copyright.show(ms_id.pageY)
                    } else {
                       comments.commit("musicID", MS_ids)
                       console.log(MS_ids)
                    }
                 })
               },
          album_ct(comments, album_id) { //获取专辑详情
               var album_ids = {
                    name: '专辑',
                    ids: [],
                    album_id: album_id
               }
               if (comments.state.music_id.album_id == album_id.album_id) {
                    return
               }
               album(album_id.album_id).then(res => {
                      for (let i = 0; i < res.data.songs.length; i++) {
                           if(res.data.songs[i].fee == 4){
                                this._vm.$PAY.show(album_ids.pageY)
                                break;
                           }
                           if(res.data.songs[i].fee == 1){
                                 this._vm.$VIP.show()
                              break;
                           }
                           album_ids.ids.push(res.data.songs[i])
                      }
                      comments.commit("album_ids", album_ids)
               })
          },
          dj_ct(comments , ids){         //获取电台详情
               var id = ids.dj_id
               if (comments.state.music_id.dj_id === id) {
                    return
               }
               dj_program_detail(id).then(res => {
                    if(res.data.program.mainSong.fee == 1){
                         this._vm.$VIP.show()
                    }else if(res.data.program.mainSong.fee == 4){
                         this._vm.$PAY.show(ids.pageY)
                    }else{
                         var dj_ids = {
                              name: '电台',
                              ids:{
                                   coverUrl:res.data.program.coverUrl,
                                   dt:res.data.program.mainSong.duration,
                                   id:res.data.program.mainSong.id,
                                   name:res.data.program.mainSong.name,
                                   dj:res.data.program.dj,
                                   type:'dj'
                              },
                              dj_id : id
                         }
                      comments.commit("dj_ids" , dj_ids)
                    }
               })
          }
     }
})